package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.ManualPickingBean;
import com.sanyang.pda.bean.ScatteredBean;
import com.sanyang.pda.bean.request.EnterToBePickedRequest;
import com.sanyang.pda.bean.request.EntireTrayConfirmRequest;
import com.sanyang.pda.contract.ScatteredContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class ScatteredPresenter extends BasePresenter<ScatteredContract.View> implements ScatteredContract.Presenter {

    @Override
    public void disassemblyList(int pageNum, String deliveryNo, String pallet) {
        NetUtils.geApi().disassemblyList(pageNum,10,deliveryNo,pallet)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<ScatteredBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<ScatteredBean>> listBaseRowsAppEntity) throws Exception {
                        getView().setList(listBaseRowsAppEntity.getRows());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void entireTrayConfirm(EntireTrayConfirmRequest entireTrayConfirmRequest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(entireTrayConfirmRequest));
        NetUtils.geApi().entireTrayConfirm(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ManualPickingBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ManualPickingBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().showMessage(manualPickingBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void enterToBePicked(EnterToBePickedRequest request) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(request));
        NetUtils.geApi().enterToBePicked(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ManualPickingBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ManualPickingBean> manualPickingBeanBaseAppEntity) throws Exception {
                        getView().showMessage(manualPickingBeanBaseAppEntity.getMsg());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
